package com.example.servletdemo.servlet.listner.session;

import com.example.servletdemo.listner.session.UserSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/UserSessionActivationServlet")
public class UserSessionActivationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //HttpSession属性的设置
        HttpSession session = req.getSession();

        UserSession user = new UserSession();
        user.setUsername("a");
        user.setPassword("123456");
//        session.setMaxInactiveInterval(30);//秒
        session.setAttribute("user",user);
        resp.setContentType("text/html;charset=utf-8");
        SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        resp.getWriter().write("添加Usersession=>"+df.format(new Date()));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
